package com.bana.fvs.protocol;

import com.bana.sys.code.SendResult;
import com.bana.sys.protocol.Protocol;
import com.bana.sys.socket.SocketResult;
import com.bana.sys.utils.Convert;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class Shoot extends Protocol {

    @Override
    public short getId()
    {
        return 0x0003;
    }

    public SocketResult handle(SocketResult sr)
    {
        if(sr.getResult()==SendResult.Success)
        {
            int result = Convert.toByte(sr.getReceive(),0);
            byte[] temp = new byte[sr.getReceive().length - 13];
            System.arraycopy(sr.getReceive(), 13, temp, 0, temp.length);
            sr.setResult(SendResult.values()[result]);
            sr.setReceive(temp);
            return sr;
        }
        else
        {
            return sr;
        }
    }

    /**
     * 获取请求字节流，用于向tcp客户端发送
     * @param buffer 需要发送的工作协议字节流
     * @param protocolId 需要等待的工作协议编号
     * @return 字节流
     */
    public byte[] request(int port,int controllerId,byte[] buffer,int protocolId) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
		dos.writeShort(port);
		dos.writeInt(controllerId);
        dos.writeInt(protocolId);
        dos.writeShort(buffer.length);
        dos.write(buffer);
        byte[] temp= baos.toByteArray();
        dos.close();
        baos.close();
        return fillHead(temp);
    }


}
